import { defineStore } from 'pinia'
import {
    getMovieList,
    uploadExcel,
    getMovieJson,
    movieDelete,
    moviesBatchDelete,
    moviesExport,
    fetchBatchProgress
} from '@/api/movie'

export const useMoviesStore = defineStore('movies', {
    state: () => ({
        movies: [],
        loading: false,
        message: '',
        currentMovie: null,
        uploadStatus: {
            uploading: false,
            success: false,
            message: ''
        },
        pagination: {
            current: 1,
            size: 10,
            total: 0
        },
        searchName: '',
        selectedMovies: [],
    }),

    actions: {
        // 设置选中的电影
        setSelectedMovies(movies) {
            this.selectedMovies = movies
        },
        // 导出选中的电影数据
        async exportMovies(ids) {
            try {
                const response = await moviesExport(ids)
                return response.data
            } catch (error) {
                this.message = '导出失败: ' + error.message
                return null
            }
        },
        // 批量删除电影
        async batchDeleteMovies(ids) {
            try {
                await moviesBatchDelete(ids)
                // 刷新列表
                await this.fetchMovies({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    name: this.searchName
                })
                this.selectedMovies = []
                return true
            } catch (error) {
                this.message = '批量删除失败: ' + error.message
                return false
            }
        },
        // 删除单个电影
        async deleteMovie(id) {
            try {
                await movieDelete(id)
                // 刷新列表
                await this.fetchMovies({
                    current: this.pagination.current,
                    size: this.pagination.size,
                    name: this.searchName
                })
                return true
            } catch (error) {
                this.message = '删除失败: ' + error.message
                return false
            }
        },
        // 获取电影列表
        async fetchMovies(params = {}) {
            this.loading = true
            try {

                const response = await getMovieList({
                    current: params.current || this.pagination.current,
                    size: params.size || this.pagination.size,
                    name: params.name || this.searchName
                })

                if (response.data.data) {
                    this.movies = response.data.data.records
                    this.pagination = {
                        current: Number(response.data.data.current),  // 确保是数字
                        size: Number(response.data.data.size),        // 确保是数字
                        total: Number(response.data.data.total)       // 确保是数字
                    }
                    this.message = `成功加载 ${response.data.data.total} 部电影`
                }
            } catch (error) {
                this.message = '获取电影列表失败: ' + error.message
            } finally {
                this.loading = false
            }
        },

        setSearchName(name) {
            this.searchName = name
            this.pagination.current = 1 // 重置到第一页
            this.fetchMovies({ current: 1, name })
        },

        // 上传Excel文件
        async uploadMovieExcel(formData) {
            this.uploadStatus.uploading = true
            try {
                const response = await uploadExcel(formData)
                this.uploadStatus.success = true
                this.uploadStatus.message = `成功处理 ${response.data.data.totalCount} 条数据`
                await this.fetchMovies() // 刷新列表
                return response.data
            } catch (error) {
                this.uploadStatus.success = false
                this.uploadStatus.message = '上传失败: ' + error.message
                throw error
            } finally {
                this.uploadStatus.uploading = false
            }
        },

        // 获取电影JSON
        async fetchMovieJson(name) {
            try {
                const response = await getMovieJson(name)
                this.currentMovie = response.data.data
                return response.data
            } catch (error) {
                this.message = '获取电影JSON失败: ' + error.message
                throw error
            }
        },

        // 获取任务进度
        async getBatchProgress(id) {
            try {
                const response = await fetchBatchProgress(id)
                return response.data
            } catch (error) {
                this.message = '获取电影JSON失败: ' + error.message
                throw error
            }
        },

        // 重置上传状态
        resetUploadStatus() {
            this.uploadStatus = {
                uploading: false,
                success: false,
                message: ''
            }
        }
    }
})